package cn.wolfcode.job.config;

import cn.wolfcode.job.FileCustomElasticJob;
import cn.wolfcode.job.FileDataflowJob;
import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class ElasticJobConfig {



    private static LiteJobConfiguration createJobConfiguration(final Class<? extends ElasticJob> jobClass,
                                                               final String cron,
                                                               final int shardingTotalCount,
                                                               final String shardingItemParameters,
                                                               final Boolean type) {
        // 定义作业核心配置
        JobCoreConfiguration.Builder jobCoreConfigurationBuilder = JobCoreConfiguration.newBuilder(jobClass.getSimpleName(), cron, shardingTotalCount);
        if(!StringUtils.isEmpty(shardingItemParameters)){
            jobCoreConfigurationBuilder.shardingItemParameters(shardingItemParameters);
        }
    
        JobTypeConfiguration jobTypeConfiguration=null;
        if(type){//dataflow
            jobTypeConfiguration= new DataflowJobConfiguration(
                    jobCoreConfigurationBuilder.build(),
                    jobClass.getCanonicalName(),
                    true);
        }else{
            jobTypeConfiguration=new SimpleJobConfiguration(jobCoreConfigurationBuilder.build(), jobClass.getCanonicalName());
        }
        // 定义SIMPLE类型配置
        //  SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(jobCoreConfigurationBuilder.build(), jobClass.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(jobTypeConfiguration).overwrite(true).build();
        return simpleJobRootConfig;
    }
//    @Bean(initMethod = "init")
//    public SpringJobScheduler initSimpleElasticJob(MyElasticJob myElasticJob,CpopoooordinatorRegistryCenter registryCenter){
//        SpringJobScheduler springJobScheduler = new SpringJobScheduler
//        (myElasticJob,registryCenter,createJobConfiguration(MyElasticJob.class,"0/3 * * * * ?",1,null));
//        return springJobScheduler;
//    }
    
//    @Bean(initMethod = "init")
//    public SpringJobScheduler initFileCustomElasticJob(FileCustomElasticJob fileCustomElasticJob, CoordinatorRegistryCenter registryCenter){
//        SpringJobScheduler springJobScheduler = new SpringJobScheduler
//        (fileCustomElasticJob,registryCenter,createJobConfiguration(FileCustomElasticJob.class,"0 0/1 * * * ?",4,"0=text,1=image,2=radio,3=vedio"));
//        return springJobScheduler;
//    }
    
    @Bean(initMethod = "init")
    public SpringJobScheduler iniDataflowElasticJob(FileDataflowJob fileDataflowJob,
                                                    CoordinatorRegistryCenter registryCenter
                                                    ,DataSource dataSource
    
    ){
  
        //时间追踪
        JobEventConfiguration jobEventConfiguration = new JobEventRdbConfiguration(dataSource);
        
        
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(fileDataflowJob,registryCenter,createJobConfiguration
                (FileCustomElasticJob.class,"0 0/1 * * * ?",1,null,true)
        ,jobEventConfiguration
        );
        return springJobScheduler;
    }

    
}